package com.listen.start;

import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.listen.vo.XunwangVO;
import com.listen.model.User;
import com.listen.service.StudyService;
import com.listen.service.UserService;
import com.listen.service.DB.GanBuXueYuanStudy;
import com.listen.service.DB.XunwangServiceImpl;
import com.listen.task.ListenTask;

public class ServiceStart {

    private static final Logger log = LoggerFactory.getLogger(ServiceStart.class);
    private static UserService userService = null;
    private static StudyService studyService = null;
    public static long SECONDS_NUMBER = 888;
    
    private static void init(String applicationName) {
        //Calendar cal = Calendar.getInstance();
        //TimeZone timeZone = cal.getTimeZone();
        //log.info("TimeZoneId:"+timeZone.getID());
        //log.info("TimeZoneDisp:"+timeZone.getDisplayName());

        log.info(String.format("开始启动【%s】服务，载入的配置文件为【%s】",applicationName, Application.SERVICE_PROVIDER_XML));
        ClassPathXmlApplicationContext context = Application.getInstance().getContext();
        userService = (UserService) context.getBean(UserService.class);
        studyService = (StudyService) context.getBean(StudyService.class);
        context.registerShutdownHook();
        context.start();
        log.info("【%s】服务实例启动完毕，请查看日志",applicationName);

    }
    public static void main(String[] args) throws Exception{
    	String appName = "省培自动听课系统";
        try{
            ServiceStart.init(appName);
            //八大员、三类、建造师
    		Thread thread1 = new Thread() {
    			public void run() {
    				log.info("30秒后启动一个轮询线程，每"+SECONDS_NUMBER+"秒轮询一次");
    				ListenTask task = new ListenTask(userService,studyService);
    				ScheduledExecutorService timer = Executors.newScheduledThreadPool(1);
    				timer.scheduleWithFixedDelay(task, 20, SECONDS_NUMBER, TimeUnit.SECONDS);
    			}
    			
    		};
    		thread1.start();
    		
    		//干部学院
    		Thread thread2 = new Thread() {
    			public void run() {
    				try {
    					ganbuxueyuan();
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
    			}
    		};
    		thread2.start();
    		
    		//讯网教学云平台
    		Thread thread3 = new Thread() {
    			public void run() {
    				try {
    					xunwang();
					} catch (Exception e) {
						e.printStackTrace();
					}
    			}
    		};
    		thread3.start();
        } catch(Exception err){
            log.error("后场服务初始化异常", err);
            log.info("服务启动异常；暂停服务，中止--pause");
            System.exit(0);
        }
        while (true) {
            try {
                Thread.currentThread();
                Thread.sleep(1L);
            } catch (Exception e) {
                log.error("后场服务运行异常", e);
            }
        }

    }
    
    public static void ganbuxueyuan() throws InterruptedException {
    	User u = new User();
        u.setZy("G");
        List<User> userList = userService.selectByUser(u);
        for(User user : userList) {
     	   user.setAddress(user.getCompanyName());
     	   user.setCookie(user.getIp());
     	   GanBuXueYuanStudy thread = new GanBuXueYuanStudy(user);
     	   thread.start();
        }
    }
    
    public static void xunwang() throws Exception {
    	User u = new User();
        u.setZy("X");
        List<User> userList = userService.selectByUser(u);
        
        User usr = null;
        for(User user : userList) {
        	XunwangServiceImpl xunwang = new XunwangServiceImpl();
        	xunwang.indexPage(user, new XunwangVO());
        	usr = user;
        }
        int i = 1;
        while(true) {
        	Thread.sleep(1000);
        	i++;
			if(i%300 == 0 && usr.isFinished()) {
				i = 0;
				usr.setStatus(1);
				xunwang();
				break;
			}
		}
    }
}
